package com.blink.zkclient.operate;

import com.blink.base.TestZK;
import com.blink.zkclient.ZkClientCreator;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * <Description>ZkClient获取子节点列表。 </Description>
 * <ClassName> GetChildrenSample</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月13日 13:33
 */
public class GetChildrenSample extends TestZK {

    @Test
    public void testGetChildren() {
        ZkClient zkClient = ZkClientCreator.getZkClient();
        String path = "/zk-book";
        zkClient.subscribeChildChanges(path, new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                logger.info(parentPath + " 's child changed, currentChilds:" + currentChilds);
            }
        });
        zkClient.createPersistent(path);
        sleep(1);
        System.out.println(zkClient.getChildren(path));
        sleep(1);
        zkClient.createPersistent(path + "/c1");
        sleep(1);
        zkClient.writeData(path + "/c1","123");
        sleep(1);
        zkClient.delete(path + "/c1");
        sleep(1);
        zkClient.delete(path);
        sleep();
        zkClient.close();
    }
}
